package com.tao.controller;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.util.List;
import javax.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import com.github.pagehelper.PageHelper;
import com.tao.entity.DataResult;
import com.tao.entity.Product;
import com.tao.service.ProductService;

@Controller
@RequestMapping("/servlet/ProductServlet")
/**
 * 商品的控制类
 * @author Administrator
 *
 */
public class ProductController {

	@Autowired
	ProductService productService;

	// 通过商品分类ID来查找该ID下的所有商品
	@RequestMapping(value = { "/findProductByListId" }, 
			params = { "id", "page" }, 
			method = RequestMethod.POST, 
			produces = "application/json;charset=utf-8")
	@ResponseBody
	public DataResult<Product> findProductByListId(
			@RequestParam(value = "id") int id,
			@RequestParam(value = "page") int page, HttpServletResponse response) {
		PageHelper.startPage(page, 10);
		List<Product> list = productService.findProductByListId(id);
		DataResult<Product> dr = new DataResult<Product>(200, list, list.size());
		return dr;
	}

	// 根据价格高低来显示商品（商品分类id）
	@RequestMapping(value = { "/listProductByPrice" }, 
			        params = { "id","page"},
			        method = RequestMethod.POST, 
			        produces = "application/json;charset=utf-8")
	@ResponseBody
	public DataResult<Product> listProductByPrice(
			@RequestParam(value = "id") int id, 
			@RequestParam(value = "page") int page,
			HttpServletResponse response) {
		PageHelper.startPage(page, 10);
		List<Product> list = productService.listProductByPrice(id);
		DataResult<Product> dr = new DataResult<Product>(200, list, list.size());
		return dr;
	}

	// 根据销量来显示商品（商品分类id）
	@RequestMapping(value = { "/listProductByStock" }, 
			params = { "id" ,"page"}, 
			method = RequestMethod.POST, 
			produces = "application/json;charset=utf-8")
	@ResponseBody
	public DataResult<Product> listProductByStock(
			@RequestParam(value = "id") int id,
			@RequestParam(value = "page") int page,
			HttpServletResponse response) {
		PageHelper.startPage(page, 10);
		List<Product> list = productService.listProductByStock(id);
		DataResult<Product> dr = new DataResult<Product>(200, list, list.size());
		return dr;
	}

	// 通过商品名称的关键字来查找所有商品
	@RequestMapping(value = { "/findProductByName" },
			params = { "name" }, 
			method = RequestMethod.POST,
			produces = "application/json;charset=utf-8")
	@ResponseBody
	public DataResult<Product> findProductByByName(
			@RequestParam(value = "name") String name,
			HttpServletResponse response) {
		try {
			name = URLDecoder.decode(name, "utf-8");
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		List<Product> list = productService.findProductByName("%" + name + "%");
		DataResult<Product> dr = new DataResult<Product>(200, list, list.size());
		return dr;
	}

	// 搜索商品根据价格高低来查找商品
	@RequestMapping(value = { "/searchProductByPrice" },
			params = { "name" }, 
			method = RequestMethod.POST, 
			produces = "application/json;charset=utf-8")
	@ResponseBody
	public DataResult<Product> searchProductByPrice(
			@RequestParam(value = "name") String name,
			HttpServletResponse response) {
		try {
			name = URLDecoder.decode(name, "utf-8");
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		List<Product> list = productService.searchProductByPrice("%" + name
				+ "%");
		DataResult<Product> dr = new DataResult<Product>(200, list, list.size());
		return dr;
	}

	// 搜索商品根据价格高低来查找商品
	@RequestMapping(value = { "/searchProductByStock" },
			params = { "name" }, 
			method = RequestMethod.POST, 
			produces = "application/json;charset=utf-8")
	@ResponseBody
	public DataResult<Product> searchProductByStock(
			@RequestParam(value = "name") String name,
			HttpServletResponse response) {
		try {
			name = URLDecoder.decode(name, "utf-8");
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		List<Product> list = productService.searchProductByStock("%" + name
				+ "%");
		DataResult<Product> dr = new DataResult<Product>(200, list, list.size());
		return dr;
	}
	
	
	

}
